<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/21
 * Time: 17:07
 */

namespace app\index\controller;

use app\extend\crypt\DesCrypt;
use app\utils\CryptUtils;

use app\system\model\UcenterMember;
use app\system\model\Member;
use app\shop\model\MemberConfig;
use app\system\model\UserMember;

use app\bbs\api\TagToFthreadApi;
use app\bbs\api\TagApi;

use app\shop\api\RedEnvelopeApi;
use app\shop\api\CouponApi;

class Test{
    //delete like message
    public function testDelLikeMsg(){
        exit('you should\'t be here');
        //您的帖子有人点赞了
        //您的回复有人点赞了
        // $model = M();
        // $r = $model->query('select m.id as mid,b.id as bid from itboye_message as m left join itboye_message_box as b on m.id = b.msg_id where m.content = \'您的回复有人点赞了\'');
        // foreach ($r as $v) {
        //     $mid = $v['mid'];
        //     $bid = $v['bid'];
        //     echo $mid.'===';
        //     echo $bid.'===';
        //     $model->execute('delete from itboye_message where id ='.$mid);
        //     $model->execute('delete from itboye_message_box where id ='.$bid);
        //     echo 'delete';
        //     echo "<br/>";
        // }
    }

    //thread likes recount
    public function testThreadLikesCount(){
        exit('you should\'t be here');
        // apiCall(ForumThreadApi::QUERY_NO_PAGING);
        // $model = M();
        // $r = $model->query('select tid,likes from itboye_forum_thread');
        // foreach ($r as $v) {
        //     $tid = $v['tid'];
        //     echo $tid.'==';
        //     $likes = intval($v['likes']);
        //     echo $likes.'==';
        //     // $r = apiCall(ForumPostLikeApi::COUNT,[['tid'=>$tid,'pid'=>0]]);
        //     $r = $model->query('select count(*) as c from itboye_forum_post_like where tid='.$tid.' and pid=0');
        //     $real_likes = intval($r[0]['c']);
        //     echo $real_likes;
        //     if($likes !== $real_likes){
        //         $r = $model->execute('update itboye_forum_thread set likes='.$real_likes.' where tid='.$tid);
        //         echo '==='.$r;
        //     }
        //     echo "\r\n";
        // }
    }
    //cache test
    public function testCache($t=0){
        exit('you should\'t be here');
        // if($t){
        //     $data = date('Y-m-d m:i:s',time());
        //     S('cachetest',$data,10);
        //     dump(S('cachetest'));
        // }else{
        //     echo 'cache';
        //     dump(S('cachetest'));
        // }
        // exit;
    }
    //unuse yet
    public function testLog(){
      exit('you should\'t be here');
      // LogRecord('im log','WARNING');
      // DbLog('支付回调优惠券发放失败 :uid:'.':tpl:');
    }
    //发送红包测试
    public function testRedEnvelope(){
        exit('you should\'t be here');
       // $r = apiCall(RedEnvelopeApi::SEND_TO_USER,[143,3,'修改红包发送测试',100,0,true]);
       // dump($r);
       // exit;
    }

    //发送优惠券测试
    public function testCoupon(){
        exit('you should\'t be here');
       // $r = apiCall(CouponApi::SEND_TO_USER,[143,1,'原始优惠券发送测试',0,0,true]);
       // dump($r);
       // exit;
    }

    //重新统计标签
    public function resetTag(){
        // $model = new \think\model\Adv();
        // $model ->startTrans();
        // 1.删除删除状态的帖子关联
        // $r = $model ->query('select GROUP_CONCAT(DISTINCT(tag.tid)) as tids from itboye_tag_to_fthread as tag left join itboye_forum_thread as t on t.`status`=-1 and t.tid=tag.tid');
        // $tids = $r[0]['tids'];
        // // dump($tids);
        // if($tids){
        //     $res = apiCall(TagToFthreadApi::DELETE,[['_string'=>'tid in ('.$tids.')']]);
        //     if(!$res['status']){
        //         $model ->rollback();
        //         echo '<br/>'.$res['info'];
        //     }
        //     echo 'delete tagtofthread:'.$res['info']; 
        // }else{
        //     echo '已全部删除';
        // }
        //2.方法1. 减少used ; bug
        //77
        // $r = $model ->query('select tag_id,count(tag_id) as count from itboye_tag_to_fthread as tag left join itboye_forum_thread as t on t.`status`=-1 and t.tid=tag.tid group by tag_id');
        // foreach ($r as $v) {
        //     echo '<br/>tag_id:'.$v['tag_id'];
        //     echo ';count:'.$v['count'];
        //     $res = apiCall(TagApi::SET_DEC2,[['id'=>$v['tag_id']],'used',$v['count']]);
        //     if(!$res['status']){
        //         $model ->rollback();
        //         echo '<br/>'.$res['info'];
        //     }
        // }
        // 
        //2.方法2：统计used ; bug
        // $r = apiCall(TagApi::QUERY_NO_PAGING);
        // if(!$r['status']){
        //     $model->rollback();
        //     echo $r['info'];
        // }else{
        //     foreach ($r['info'] as $v) {
        //         $used = $v['used'];
        //         $id   = $v['id'];
        //         $res = apicall(TagToFthreadApi::COUNT,[['tag_id'=>$id]]);
        //         if(!$r['status']){
        //             $model->rollback();
        //             echo $r['info'];
        //         }else{
        //             $count = $res['info'];
        //             echo '<br/>'.$count.'-'.$used;
        //             if($count != $used){
        //                 $res = apiCall(TagApi::SAVE,[['id'=>$id],['used'=>$count]]);
        //                 if(!$r['status']){
        //                     $model->rollback();
        //                     echo $r['info'];
        //                 }else{
        //                     echo 'save'.$id;
        //                 }
        //             }
        //         }
        //     }
        // }
        // $model->commit();
        echo '<br/>success';
    }

    public function addUsers(){
        exit("you should't be here ");
        
        // ignore_user_abort(true);
        // set_time_limit(0);
        // $num = intval($_GET["num"]);
        // $type = 1;
        // $from = 0;
        // $suc  = 0;
        // $trans = new \think\model\Adv();
        // $trans->startTrans();
        // $flag = false;
        // $now  = time();
        // for ($i=0; $i < $num ; $i++) {
        //   $username = "M_lTEST_".$i;
        //   $password = "e56d09a4e36a3281d98176728bf24683";
        //   $nickname = $username;
        //   //写入第一张表 UcenterMember
        //   $model = new UcenterMember();
        //   $entity = [
        //     'username'        =>$username,
        //     'password'        =>$password,
        //     'mobile'          =>'',
        //     'email'           =>'',
        //     'reg_time'        =>$now,
        //     'reg_ip'          =>0,
        //     'last_login_time' =>0,
        //     'last_login_ip'   =>0,
        //     'update_time'     =>$now,
        //     'status'          =>1,
        //     'reg_from'        =>0,
        //     'weixin_uid'      =>'',
        //   ];
        //   $uid = $model ->add($entity);
        //   // dump($uid);
        //   //写入第二张表 common_member
        //   $model2 = new Member();
        //   $map = [
        //       'uid'         => $uid,
        //       'realname'    => '',
        //       'idnumber'    => '',
        //       'nickname'    => $nickname,
        //       'sex'         => 0,
        //       'birthday'    => $now,
        //       'qq'          => '',
        //       'head'        => 0,
        //       'score'       => 0,
        //       'login'       => 0,
        //       'update_time' => $now, //auto-model bug
        //       'status'      => 1,    //auto-model bug
        //       'add_uid'     => 0,
        //       'sign'        => '',
        //   ];
        //   $r = $model2 ->add($map);
        //   dump($r);
        //   //插入到第三张表
        //   $model3 = new MemberConfig();
        //   $map = [
        //       'uid'               =>$uid,
        //       'phone_validate'    =>0,
        //       'email_validate'    =>0,
        //       'idcode'            =>'',
        //       'wxopenid'          =>'',
        //       'identity_validate' =>0,
        //       'invite_id'         =>'',
        //   ];
        //   $model3 ->add($map);
        //   //插入到第四张表
        //   $model4 = new UserMember();
        //   $map = [
        //     'uid'           =>$uid,
        //     'group_id'      =>1,
        //     'active_status' =>0,
        //     'active_time'   =>$now,
        //   ];
        //   $model4 ->add($map);
        //   echo "添加用户".$uid.":".$username."成功<br/>";
        // }
        // exit('结束');
    }

    public function index(){
        // header("Content-type: text/html; charset=utf-8");
        $encode = "ZXlKamIyUmxJam94TURBd0xDSmtZWFJoSWpvaVhIVTNaak5oWEhVMU9UTXhZMnhwWlc1MFgybGtJU0lzSW01dmRHbG1lVjlwWkNJNklpSjk=";
        // $encode = "ZXlKamIyUmxJam94TURBd0xDSmtZWFJoSWpvaVhIVTNaak5oWEhVMU9UTXhZMnhwWlc1MFgybGtJU0lzSW01dmRHbG1lVjlwWkNJNklpSjk=";
        echo base64_decode(base64_decode($encode));exit;
//        $str = '{"app_version":10003,"time":"1461330243","data":"ZXdvZ0lDSjFjMlZ5Ym1GdFpTSWdPaUFpYVhSaWIzbGxJaXdLSUNBaWNHRnpjM2R2Y21RaUlEb2dJakV5TXpRMU5pSUtmUT09","alg":"md5","app_type":"ios","type":"BY_User_login","api_ver":100,"notify_id":"1461330243598","sign":"0c00a3e7fdab57db11bd1210ed3dfe38"}';
//        echo '<br/>'.$str;
//        $str = rtrim($str,"\0 ");
//        echo '<br/>'.$str;
//        $str = rtrim($str,"\\0");
//        echo '<br/>'.$str;
//        $str = rtrim($str," ");
//
//        echo '<br/>'.$str;
//
//        dump(json_decode($str));
//            $content = "hebidu何必都HEBIDU33123";
        $key = "b6b27d3182d589b92424cac0f2876fcd";
//        echo "<BR/>明文 ".$content;
//
//         echo "<BR/>加密后 ". base64_encode(DesCrypt::encode($content,$key));
//
//        $encode="yVnyhMXsSpba83WB5FeCBSuc+HAsJJLDDyoX6zSpVWk=";
        // $encode = "5YGEJ880KgykdutemRToU9ZChLbqkUs35YfJbHH99PC8gjrUfaKpHPFYzJTWt6t957S4QFeVutFCr56Vj/p8DzZVfU2ScAgXW82l7TkVNhTMhWa1T6Plwqc9M7oG7Hyfb+9Tha0mqTLmhj9tUlNQ2fMJ9Sp3CcQ4mT4NibjnCj300q6x97Ek1ZyETTiOXCweGox0hCQ4uSQAejARRMhVhFkPwzGO41aAf68WBXlygQ2IEmWv8OrzgIkzctxxbvQIxM3WctMxey1GPdtuwVUXnraug6rTYtbNw+jIf6YaWa1Z4Te6tLJWAwyDEeH2+e+ZM8i42LEp55R1xx//R7BRGH7whRnWK81C";

        $decode = DesCrypt::decode(base64_decode($encode),$key);
        echo strlen($decode);
        $decode = chop($decode);
        echo "<br/><br/>";


        echo $decode;
        dump(json_decode($decode,JSON_OBJECT_AS_ARRAY));

        return "END";
    }

}